//
//  RXWellcomeController.swift
//  weibo
//
//  Created by uncle-R on 15/12/17.
//  Copyright © 2015年 uncle-R. All rights reserved.
//

import UIKit
import SDWebImage

class RXWellcomeController: UIViewController {
    
      private var iconViewBottomCon: NSLayoutConstraint?

    private  var bottomContraint :NSLayoutConstraint = NSLayoutConstraint()

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //添加控件
        prepareUI()
        //2.先显示上一次保存的头像
        setIconView()
        //加载最新的头像
        RXUserAccount.loadUserAccount()?.loadUserInfo({ (error) -> () in
            if error != nil {
            print("获取用户信息出错")
            }
            //获取用户数据没有出错.加载最新头像
            self.setIconView()
        })
        
        
        //加载最新头像
    }
    //显示当前用户头像
    private func setIconView(){
        //获取当前用户头像的地址
        if let avage_large = RXUserAccount.loadUserAccount()?.avatar_large{
            
            let url = NSURL(string: avage_large)
            //http://tp2.sinaimg.cn/3888312881/180/5741752383/1
            //print("url========================================== \(url)")
            
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            
        }

    }
    //当view看的到的时候做动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //在view即将要显示出来的时候做动画
        //改变头像距离底部的位置的约束做动画
        
        iconViewBottomCon?.constant = -(UIScreen.mainScreen().bounds.size.height - 160)
        
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue:0), animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in //_表示占位
                
                self.welcomeLabel.alpha = 0
                //头像完成之后显示lable
                
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    self.welcomeLabel.hidden = false
                    }, completion: { (_) -> Void in
                       
                        //显示完lable跳转到主控制器界面.
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
                })
        }
        
    }
    
    /**
     加载子控件
     */
    func prepareUI(){
        
        
        view.addSubview(backgroundView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
//        backgroundView.translatesAutoresizingMaskIntoConstraints = false
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        //设置背景约束位置
        backgroundView.ff_Fill(self.view)
        
        //头像,水平和父控件重合
       let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: -160))
        iconViewBottomCon = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
         welcomeLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
//        view.addConstraint(NSLayoutConstraint(item: backgroundView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: backgroundView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: backgroundView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: backgroundView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
//        
//        //设置头像的约束
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//         bottomContraint =  NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
//        view.addConstraint(bottomContraint)
//        //设置头像的宽高约束
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil,attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//        
//        //设置 "欢迎回来的" 参照 头像的位置
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))

        
    }
    
    
    
    
    
//MARK: - 控件的懒加载
    //背景
  private lazy var backgroundView :UIImageView = {
        let view = UIImageView()
        view.image = UIImage(named: "ad_background")
        return view
    }()
    //头像
   private lazy var iconView :UIImageView = {
        let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        //将图片切成圆的
        view.layer.cornerRadius = 42.5
        view.layer.masksToBounds = true
        return view
    }()
    private lazy var welcomeLabel :UILabel = {
        let label = UILabel()
        label.hidden = true
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(14)
        return label
    }()
}

















